package com.bw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.result.ResponseResult;
import com.bw.user.mapper.TbUserMapper;
import com.bw.user.pojo.TbBankInfo;
import com.bw.user.pojo.TbLinkmanAuth;
import com.bw.user.mapper.TbLinkmanAuthMapper;
import com.bw.user.pojo.TbUser;
import com.bw.user.service.TbLinkmanAuthService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 联系人认证表 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbLinkmanAuthServiceImpl extends ServiceImpl<TbLinkmanAuthMapper, TbLinkmanAuth> implements TbLinkmanAuthService {

    @Autowired
    private TbLinkmanAuthMapper linkmanAuthMapper;

    @Autowired
    private TbUserMapper userMapper;

    @Override
    public ResponseResult linkauth(TbLinkmanAuth tbLinkmanAuth) {

        QueryWrapper<TbLinkmanAuth> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",tbLinkmanAuth.getUserId());

        List<TbLinkmanAuth> linkmanAuths = linkmanAuthMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(linkmanAuths)){
            return ResponseResult.error("您的联系人已经认证了.无法再次认证");
        }

        if (StrUtil.isEmpty(tbLinkmanAuth.getUrgencyLinkmanName())){
            return ResponseResult.error("请输入紧急联系人姓名");
        }
         if (StrUtil.isEmpty(tbLinkmanAuth.getUrgencyLinkmanPhone())){
            return ResponseResult.error("请输入紧急联系人手机号");
        }
         if (ObjectUtil.isNull(tbLinkmanAuth.getUrgencyLinkmanRelation())){
            return ResponseResult.error("请输入紧急联系人与联系人关系");
        }
         if (StrUtil.isEmpty(tbLinkmanAuth.getFirstLinkmanName())){
            return ResponseResult.error("请输入第一联系人姓名");
        }
         if (StrUtil.isEmpty(tbLinkmanAuth.getFirstLinkmanPhone())){
            return ResponseResult.error("请输入第一紧急联系人电话");
        }
         if (ObjectUtil.isNull(tbLinkmanAuth.getFirstLinkmanRelation())){
            return ResponseResult.error("请输入第一联系人与联系人关系");
        }
         if (StrUtil.isEmpty(tbLinkmanAuth.getSecondLinkmanName())){
            return ResponseResult.error("请输入第二联系人姓名");
        }
         if (StrUtil.isEmpty(tbLinkmanAuth.getSecondLinkmanPhone())){
            return ResponseResult.error("请输入第二联系人电话");
        }
         if (ObjectUtil.isNull(tbLinkmanAuth.getSecondLinkmanRelation())){
            return ResponseResult.error("请输入第二联系人与联系人关系");
        }

        tbLinkmanAuth.setCreateTime(new Date());
        tbLinkmanAuth.setIsDelete(0);
        tbLinkmanAuth.setUpdateTime(new Date());

        linkmanAuthMapper.insert(tbLinkmanAuth);
        return new ResponseResult(200,"添加联系人成功!");
    }
}
